{{- $attributes := "" -}}
{{- $render := false -}}
{{- $title := "" -}}
{{- $filename := "" -}}
{{- $file := "" -}}

{{- range $key, $val := .Attributes -}}
    {{- $attributes = printf "%s %s=%s" $attributes (string $key) (string $val) -}}
    {{- if and (eq $key "render") (eq $val true) -}}
        {{- $render = true -}}
    {{- end -}}
    {{- if (eq $key "title") -}}
        {{- $title = $val -}}
    {{- end -}}
    {{- if (eq $key "file") -}}
        {{- $filename = $val -}}
        {{- $file = readFile $val -}}
    {{- end -}}
{{- end -}}

{{- if and (eq $title "auto") (ne $filename "") -}}
    {{- $title = $filename -}}
    {{- $attributes = $attributes | replaceRE "title=.+" (printf "title=%s" $title) -}}
{{- end -}}

{{- $codespan := highlight (cond (ne $filename "") $file .Inner) .Type -}}
{{- $codespan = $codespan | replaceRE "(^<div class=\"highlight\">|</div>$)" "" -}}

<div class="highlight" {{ $attributes | safeHTMLAttr }}>
    {{- if $title -}}
    <div class="title">
        {{- $title | $.Page.RenderString (dict "display" "inline") -}}
    </div>
    {{- end -}}
    {{ $codespan | safeHTML }}
    {{- if $render -}}
    <div class="result">
        {{- partial "func/render" (dict "str" .Inner "root" .Page) -}}
    </div>
    {{- end -}}
    <span class='board'>
        <img class='edit' src='/img/edit.svg'>
        <img class='clipboard' src='/img/clipboard.svg'>
    </span>
</div>
